package com.shujiaotong.handler;



import com.shujiaotong.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

// 异常捕获器
@RestControllerAdvice
@Slf4j
public class GlobalHandler {

  // 捕获指定异常
  @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
  public R idx_username(SQLIntegrityConstraintViolationException e){
    log.info("用户行为异常{}",e.getMessage());
    String msg = e.getMessage();
    String[] s = e.getMessage().split(" ");
    if(s.length>=3){
      msg = s[2]+"用户名已存在";
    }
//    e.printStackTrace();
    return R.error(msg);
  }

}
